package com.gl.order.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: liangSY
 * @Date: 2021/6/11 16:49
 * @ClassName: SwaggerConfig
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig  {

    @Bean
    public Docket createRestApi(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.gl.order.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(requestParameters())
                ;
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .version("1.0")
                .build();
    }

   private List<Parameter> requestParameters(){
       List<Parameter> pars = new ArrayList<>();
        pars.add(new ParameterBuilder()
                .name("Authorization") // 参数名
                .description("user token")  //描述
                .modelRef(new ModelRef("String")) //指定参数值的类型
                .parameterType("header") // 参数类型支持header, cookie, body, query etc
                .defaultValue("Bearer ")  // 默认值
                .build());
        return pars;
    }

}
